/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.security.handler;

import vip.dexian.admin.manager.AdminManager;
import vip.dexian.common.Message;
import vip.dexian.common.utils.WebUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 认证成功处理
 *
 * @author 挺好的 2023年06月06日 15:32
 */
@Component ("simpleAuthenticationSuccessHandler")
public class SimpleAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    /**
     * 管理员
     */
    @Resource (name = "adminManagerImpl")
    private AdminManager adminManager;

    /**
     * 登录成功的操作
     *
     * @param request
     *         {@link jakarta.servlet.http.HttpServletRequest}
     * @param response
     *         {@link jakarta.servlet.http.HttpServletResponse}
     * @param authentication
     *         认证信息
     *
     * @throws java.io.IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess (HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

        this.adminManager.loginSuccess(WebUtils.getIPAddr(request));

        WebUtils.responseMessage(response, Message.success());
    }
}
